package fy.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.v3x.webmail.domain.MailInfo;
import com.seeyon.v3x.webmail.manager.MailManager;

public class MyUtil {
	public static int accountDay(String early, String last) {
		int day = 0;
		if (early != null && last != null) {
			day = (int) (Timestamp.valueOf(last + " 00:00:00").getTime() - Timestamp
					.valueOf(early + " 00:00:00").getTime())
					/ (1000 * 60 * 60 * 24);
		}
		return day;
	}

	public static String getYear() {
		return (new Timestamp(Calendar.getInstance().getTimeInMillis()))
				.toString().substring(0, 4);
	}

	public static String clearEmpty(String s) {
		return s == null ? null : s.replaceAll(" ", "");
	}

	public static String getDate(String time) {
		return time.indexOf(" ") == -1 ? time : time.substring(0,
				time.indexOf(" "));
	}

	/**
	 * 判断字符串是否为null或者""
	 * 
	 * @param s
	 * @return 为空返回false
	 */
	public static boolean strIsNotNull(String s) {
		return s != null && !"".equals(s);
	}

	/**
	 * 比较时间
	 * 
	 * @param time1
	 * @param time2
	 * @return boolean 如果为true则 time1> time2
	 */
	public static boolean later(String timestamp1, String timestamp2) {
		return (Timestamp.valueOf(timestamp1).getTime() > Timestamp.valueOf(
				timestamp2).getTime());
	}

	public static boolean later(String date1, String time1, String date2,
			String time2) {
		return later(new StringBuilder().append(date1).append(" ")
				.append(time1).toString(), new StringBuilder().append(date2)
				.append(" ").append(time2).toString());
	}

	public static String yesterday(String date) {
		String time = new Timestamp(Timestamp
				.valueOf(
						new StringBuilder().append(date).append(" 00:00:00")
								.toString()).getTime() - 1000 * 60 * 60)
				.toString();
		return time.substring(0, time.indexOf(' '));
	}

	public static String tomorrow(String date) {
		String time = new Timestamp(Timestamp
				.valueOf(
						new StringBuilder().append(date).append(" 23:59:59")
								.toString()).getTime() + 1000 * 60 * 60)
				.toString();
		return time.substring(0, time.indexOf(' '));
	}

	public static String getEarlyDay(List<String> dateList) {
		String early = null;
		long time = 0;

		for (int i = 0, size = dateList.size(); i < size; i++) {
			if (early == null) {
				early = dateList.get(i);

				time = Timestamp.valueOf(
						new StringBuilder().append(early).append(" 00:00:00")
								.toString()).getTime();
			} else {
				String temp = dateList.get(i);
				long tempTime = Timestamp.valueOf(
						new StringBuilder().append(temp).append(" 00:00:00")
								.toString()).getTime();

				if (tempTime < time) {
					early = temp;
					time = tempTime;
				}
			}
		}
		return early;
	}

	public static String getLastDay(List<String> dateList) {
		String last = null;
		long time = 0;
		for (int i = 0, size = dateList.size(); i < size; i++) {
			if (last == null) {
				last = dateList.get(i);

				time = Timestamp.valueOf(
						new StringBuilder().append(last).append(" 00:00:00")
								.toString()).getTime();
			} else {
				String temp = dateList.get(i);
				long tempTime = Timestamp.valueOf(
						new StringBuilder().append(temp).append(" 00:00:00")
								.toString()).getTime();

				if (tempTime > time) {
					last = temp;
					time = tempTime;
				}
			}
		}
		return last;
	}

	public static String getNowStr() {
		String now = (new Timestamp(Calendar.getInstance().getTimeInMillis())
				.toString());
		return now.substring(0, now.indexOf(' '));
	}

	public static String getTodayDateStr() {
		String today = (new Timestamp(Calendar.getInstance().getTimeInMillis())
				.toString());
		return today.substring(0, today.indexOf(' '));
	}

	public static BufferedReader createBufferedReader(String path,
			String encoding) throws Exception {
		return new BufferedReader(new InputStreamReader(new FileInputStream(
				path), encoding));
	}

	public static BufferedReader createBufferedReader(InputStream file,
			String encoding) throws Exception {
		return new BufferedReader(new InputStreamReader(file, encoding));
	}

	public static BufferedReader createBufferedReader(String path)
			throws Exception {
		return createBufferedReader(path, "utf-8");
	}

	public static String getLimitLengthString(String str, int limitLength,
			String symbol) throws Exception {
		if (str == null) {
			return "";
		}
		byte[] b = str.getBytes("utf-8");
		int len = b.length;

		if (len <= limitLength) {
			return str;
		} else {
			limitLength = limitLength - limitLength % 3
					- symbol.getBytes("utf-8").length;
			return new StringBuilder()
					.append(new String(b, 0, limitLength, "utf-8"))
					.append(symbol).toString();
		}
	}

	public static String createUUID() {
		String uuid = String.valueOf(UUIDLong.longUUID());
		int length = uuid.length();
		if (length < 20) {
			StringBuilder sb = new StringBuilder().append(uuid);

			String time = String.valueOf(Calendar.getInstance()
					.getTimeInMillis());
			int first = time.length() - (20 - length);
			sb.append(time.substring(first < 0 ? 0 : first, time.length()));
			while (sb.length() < 20) {
				sb.append("X");
			}

			return sb.toString();
		} else {
			return uuid;
		}
	}

	public static boolean isExist(List<String> list, String str) {
		boolean exist = false;

		for (int i = 0, size = list == null ? 0 : list.size(); i < size; i++) {
			if (str.equals(list.get(i))) {
				exist = true;
				break;
			}
		}

		return exist;
	}

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 返回类的路径
	 * 
	 * @param c
	 *            Class
	 * @return
	 */
	public static String getRealPath(Class c) {
		return c.getResource(
				new StringBuilder().append("/")
						.append(c.getPackage().getName().replace(".", "/"))
						.append("/").toString()).getPath();
	}

	public static String getRealPath(String name) {
		return MyUtil.class.getResource(
				new StringBuilder()
						.append("/")
						.append(name.substring(0, name.lastIndexOf("."))
								.replace(".", "/")).append("/").toString())
				.getPath()
				+ name.substring(name.lastIndexOf(".") + 1, name.length());
	}

	public static String replaceXMLString(String str) {
		return str == null || "".equals(str) ? str : (str
				.replace("&apos;", "'").replace("&quot;", "\"")
				.replace("&lt;", "<").replace("&gt;", ">")
				.replace("&amp;", "&"));
	}

	public static int indexOf(String[] array, String str) {
		if (array != null) {
			for (int i = 0, length = array.length; i < length; i++) {
				if (str != null && str.equals(array[i])) {
					return i;
				}
			}
		}
		return -1;
	}

	// public static boolean hasUpdateDelete(String str){
	// if(str.indexOf("delete")>-1||str.indexOf("update")>-1)
	// }

	public static void createFile(String fileName, String content)
			throws Exception {
		FileOutputStream fos = new FileOutputStream(fileName);
		fos.write(content.getBytes("utf-8"));
		fos.flush();
		fos.close();
	}

	public static boolean sendMail(MailInfo mi) {
		try {
			return MailManager.sendMail("192.168.254.8",
					"oasystem@fuyaogroup.com", "fuyao2015mail", mi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void main(String[] args) {

		try {
			System.out.println(createUUID());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//补秒
	  public static String getAddZero(String time) {
		  if(time.length()==5){
			StringBuilder sb = new StringBuilder();
			sb.append(time);
			sb.append(":00");
			time=sb.toString();}
			return time;
		}
}
